package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.SupplyContract;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.exception.LogicException;
import com.lkd.feign.VMService;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author: cl
 * @Date: 2022/6/25 - 06 - 25
 * @Description:
 */

//运维工单
@Component
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
public class TaskCompleteHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vendingMachineService;


    @Override
    public void process(String jsonMsg) throws IOException {


        if (jsonMsg.isBlank()) throw new LogicException("消息为空");

        //反序列化
        TaskCompleteContract taskCompleteContract = JsonUtil.getByJson(jsonMsg, TaskCompleteContract.class);

        if (taskCompleteContract == null) throw new LogicException("消息反序列化失败！");

        //判断是维修投放  还是撤机
        if (taskCompleteContract.getTaskType() == VMSystem.TASK_TYPE_DEPLOY ||
                taskCompleteContract.getTaskType() == VMSystem.TASK_TYPE_REPAIR) {
            //更新状态为运营
            vendingMachineService.updateStatus(taskCompleteContract.getInnerCode(),
                    VMSystem.VM_STATUS_RUNNING);
        }

        if (taskCompleteContract.getTaskType() == VMSystem.TASK_TYPE_REVOKE) {
            //更新状态为撤机
            vendingMachineService.updateStatus(taskCompleteContract.getInnerCode(),
                    VMSystem.VM_STATUS_REVOKE);
        }
    }
}
